require 'will_paginate'

class OptionsController < ApplicationController
  
  def initialize
    @orgaos = Organ.find :all,:joins => ["join opcao on opcao.orgao = orgao.id"]
    @niveis = Level.find :all
    @tiposopcoes = Opcaotipo.find :all
    @cursos = Curso.find :all
    @regimesletivos = Regimeletivo.find :all
    @areasbasicas = Basicarea.find :all
    @conhecimentosareas = Conhecimentoarea.find :all
    @avaliacaoareas = Organ.find :all
    #@docentes = Docente.find :all
  end

  # GET /options
  # GET /options.xml
  def index
    
    @options = ConsultaOption.options params[:orgao],params[:nivel],params[:ativa],params[:page]

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @options }
    end
  end

  # GET /options/1
  # GET /options/1.xml
  def show
    @option = Option.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @option }
    end
  end

  # GET /options/new
  # GET /options/new.xml
  def new
    @option = Option.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @option }
    end
  end

  # GET /options/1/edit
  def edit
    @option = Option.find(params[:id])
  end

  # POST /options
  # POST /options.xml
  def create
    @option = Option.new(params[:option])

    respond_to do |format|
      if @option.save
        flash[:notice] = 'Option was successfully created.'
        format.html { redirect_to(options_url) }
        format.xml  { render :xml => @option, :status => :created, :location => @option }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @option.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /options/1
  # PUT /options/1.xml
  def update
    @option = Option.find(params[:id])

    respond_to do |format|
      if @option.update_attributes(params[:option])
        flash[:notice] = 'Option was successfully updated.'
        format.html { redirect_to(options_url) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @option.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /options/1
  # DELETE /options/1.xml
  def destroy
    @option = Option.find(params[:id])
    @option.destroy

    respond_to do |format|
      format.html { redirect_to(options_url) }
      format.xml  { head :ok }
    end
  end
end
